//
//  AOTabBar.swift
//  weibo_zha
//
//  Created by moxuan on 16/10/22.
//  Copyright © 2016年 渣. All rights reserved.
//

import UIKit

class AOTabBar: UITabBar {
    //创建闭包
    var closure: (()->())?
    
    override init(frame: CGRect){
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //设置UI
    private func setupUI() {
    addSubview(composeButton)
    }
    

    //点击事件
    @objc private func buttonClick() {
        closure?()
    }
    
    //修改的样式
    override func layoutSubviews() {
        super.layoutSubviews()
        //设置宽度
        let w = AOScreeW * 0.2
        var index: CGFloat = 0
        //遍历subview
        for value in subviews {
            //判断是否为想要的UITabBarButton
            if value.isKind(of: NSClassFromString("UITabBarButton")!) {
                //设置x y
                value.frame.size.width = w
                value.frame.origin.x = w * index
                //计数加一
                index += 1
                //撰写按钮位置
                if index == 2 {
                    index += 1
                }
            }
        }
       //设置撰写按钮位置
        composeButton.center.x = AOScreeW * 0.5
        composeButton.center.y = frame.size.height * 0.5
    }
    
    
    //懒加载，控件
    lazy var composeButton: UIButton = {
        let button = UIButton(setImgName: "tabbar_compose_icon_add", backgroundImgName: "tabbar_compose_button", target: self, action: #selector(buttonClick))
        return button
    }()
}
